/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: BaseController
 * Author:   xi
 * Date:     2019/8/17 12:48
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.supconit.controller;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author xi
 * @create 2019/8/17
 * @since 1.0.0
 */

import com.alibaba.fastjson.JSON;
import com.supconit.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public abstract class BaseController {
    @Autowired
    private HttpServletRequest request;

    public BaseController() {
    }

    public Object renderSuccess(Integer code, String msg, Object obj) {
        Result result = new Result();
        result.setSuccess(true);
        result.setMsg(msg);
        result.setCode(code);
        result.setObj(obj);
        return result;
    }

    public Object renderSuccess() {
        return this.renderSuccess(Integer.valueOf(1), "操作成功", (Object)null);
    }

    public Object renderSuccess(String msg) {
        return this.renderSuccess(Integer.valueOf(1), msg, msg);
    }

    public Object renderSuccess(Object obj) {
        return this.renderSuccess(Integer.valueOf(1), "操作成功", obj);
    }

    public Object renderSuccess(String msg, Object obj) {
        return this.renderSuccess(Integer.valueOf(1), msg, obj);
    }

    public Object renderSuccess(Integer code, String msg) {
        return this.renderSuccess(code, msg, (Object)null);
    }

    public Object renderError(Integer code, String msg, Object obj) {
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(code);
        result.setObj(obj);
        return result;
    }

    public Object renderError() {
        return this.renderError(Integer.valueOf(-1), "操作失败", (Object)null);
    }

    public Object renderError(String msg) {
        return this.renderError(Integer.valueOf(-1), msg, msg);
    }

    public Object renderError(Object obj) {
        return this.renderError(Integer.valueOf(-1), "操作失败", obj);
    }

    public Object renderError(String msg, Object obj) {
        return this.renderError(Integer.valueOf(-1), msg, obj);
    }

    public Object renderError(Integer code, String msg) {
        return this.renderError(code, msg, (Object)null);
    }

    public <T> T getUser(Class<T> user) {
        String info = this.request.getHeader("user");
        return info != null && !"".equals(info)? JSON.parseObject(info, user):null;
    }
}